package com.dongdongshop.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.pojo.Cart;
import com.dongdongshop.pojo.TbOrderItem;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.CartService;
import com.dongdongshop.util.CookieUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("cart")
public class CartController {
    @Reference
    private CartService cartService;

    //查询购物车
    @RequestMapping("cartlist")
    @ResponseBody
    public Map<String,Object> cartList(HttpServletRequest request,HttpServletResponse response){
        TbUser user = (TbUser)SecurityUtils.getSubject().getPrincipal();
        //从cookie中查询
        String cartlist = CookieUtils.getCookieValue(request, "list", "utf-8");
        //如果是第一次,为null或者为""
        if (StrUtil.hasBlank(cartlist)){
            cartlist = "[]";
        }
        //cookie中查询的
        List<Cart> carts = JSONObject.parseArray(cartlist, Cart.class);
        if (user == null){
            Map<String, Object> map = getMap(carts);
            return map;
        }else{
            List<Cart> list = cartService.findCartListFromRedis(user.getUsername());
            if (carts.size() >0){
                //合并redis和cookie中的数据
                List<Cart> carlist =  cartService.mergeCartListCookieAndReids(carts,list);
                cartService.saveCartListRedis(user.getUsername(),carlist);
                CookieUtils.deleteCookie(request,response,"list");
                Map<String, Object> listMap = getMap(list);
                return listMap;
            }
            Map<String, Object> map = getMap(list);
            return map;
        }
    }

    public Map<String,Object> getMap(List<Cart> list){
        BigDecimal total = new BigDecimal(0.00);
        for (Cart cart : list) {
            List<TbOrderItem> slist = cart.getList();
            for (TbOrderItem orderItem : slist) {
                BigDecimal each = orderItem.getTotalFee();
                total = total.add(each);
            }
        }
        Map<String,Object> map = new HashMap<>();
        map.put("total",total);
        map.put("list",list);
        return map;
    }

    //进行增加购物车
    @RequestMapping("addCart")
    public String addCart(Long itemId, Integer num, HttpServletRequest request, HttpServletResponse response){
        Map<String, Object> map = cartList(request, response);
        //调用service层进行添加购物车,添加完成之后将购物车进行返回
        List<Cart> carts = (List<Cart>)map.get("list");
        carts  = cartService.addCart(carts,itemId,num);
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();  if (user == null){
            CookieUtils.setCookie(request,response,
      "list",JSONObject.toJSONString(carts),true);
        }else{
            cartService.saveCartListRedis(user.getUsername(),carts);
        }
        return "redirect:/toCart";
    }
}
